package com.tjp.eight.ui.fragment.zuji;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tjp.eight.R;
import com.tjp.eight.ui.fragment.detail.HistoryModel;
import com.tjp.eight.utils.ActivityUtils;
import com.tjp.eight.views.CircleImageView;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/3/27.
 */
public class HistoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;

    ArrayList<HistoryRModel.HistoryItem> models;

    public HistoryAdapter(Context context) {
        this.context = context;
        models = new ArrayList<>();
    }

    public void addData(ArrayList<HistoryRModel.HistoryItem> historyItems) {
        if (historyItems == null || historyItems.isEmpty()) return;
        int index = models.size();
        for (HistoryRModel.HistoryItem item : historyItems) {
            models.add(item);
        }
        notifyItemRangeInserted(index, getItemCount());
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_history, parent, false);
        return new HistoryViewHolder(view);
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final String uid = models.get(position).uid;
        ((HistoryViewHolder) holder).view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtils.startOtherHomePage(context, uid);
            }
        });
        Glide.with(context).load(models.get(position).iconurl).error(R.drawable.icon_login).into(((HistoryViewHolder) holder).civ_msg_user);
        ((HistoryViewHolder) holder).tv_msg_who_tips.setText("浏览了这篇文章");
        ((HistoryViewHolder) holder).tv_msg_who.setText("" + models.get(position).name);
        ((HistoryViewHolder) holder).tv_pass_time.setText("" + models.get(position).time);

    }

    @Override
    public int getItemCount() {
        return models.size();
    }

    private class HistoryViewHolder extends RecyclerView.ViewHolder {
        View view;
        CircleImageView civ_msg_user;
        TextView tv_msg_who;
        TextView tv_msg_who_tips;
        TextView tv_pass_time;

        public HistoryViewHolder(View view) {
            super(view);
            this.view = view;
            civ_msg_user = (CircleImageView) view.findViewById(R.id.civ_msg_user);
            tv_msg_who = (TextView) view.findViewById(R.id.tv_msg_who);
            tv_msg_who_tips = (TextView) view.findViewById(R.id.tv_msg_who_tips);
            tv_pass_time = (TextView) view.findViewById(R.id.tv_pass_time);
        }
    }
}
